package cn.jsony.org.controller;

import cn.jsony.basic.config.BusinessException;
import cn.jsony.basic.util.AjaxResult;
import cn.jsony.basic.util.ExcelUtils;
import cn.jsony.basic.util.PageList;
import cn.jsony.org.domain.Shop;
import cn.jsony.org.domain.ShopAuditLog;
import cn.jsony.org.domain.ShopDto;
import cn.jsony.org.query.ShopQuery;
import cn.jsony.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "商店的API",description="商店相关的CRUD功能")
@RestController//@responsebody+@controller
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService service;

    //查所有-get
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Shop> findAll(){

        System.out.println("查找所有店铺");


        return service.findAll();
    }




    @GetMapping("/{id}")//查找一个get
    @ApiOperation(value = "通过ID查询" ) //：打在方法上，对方法做描述
    public Shop findOne(@PathVariable("id") Long id){
        return (Shop) service.findOne(id);

    }
    //分页查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery){

        return service.queryPage(shopQuery);
    }

    //删除
    @ApiOperation(value = "删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            service.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    //批量删除
    @ApiOperation(value = "批量删除" )
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }




    //修改and添加
    @ApiOperation(value = "修改and添加" )
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                service.add(shop);
            }else {
                service.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    //店铺入驻
    @ApiOperation(value = "店铺入驻" )
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            service.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败失败"+e.getMessage());
        }catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，稍后再试"+e.getMessage());
        }
    }

    //审核通过
    @ApiOperation(value = "审核通过" )
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            service.auditPass(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙500"+e.getMessage());
        }
    }

    //审核不通过
    @ApiOperation(value = "审核不通过" )
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            service.auditReject(shopAuditLog);
            return new AjaxResult();
        } catch (Exception e) { //500
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙500"+e.getMessage());
        }
    }

    @ApiOperation(value = "导出" )
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        try {
            List<Shop> shops = service.findAll();
            /*(ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);*/
            ExcelUtils.exportExcel(shops,null,"店铺信息",Shop.class,"Shop.xlsx",response);
        } catch (Exception e) { //500
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "导入" )
    @PostMapping("/importExcel")//对复杂表单处理
    public void importExcel(@RequestPart("file")MultipartFile file){
        try {
            List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
            for (Shop shop : shops) {
                System.out.println(shop);
            }
        } catch (Exception e) { //500
            e.printStackTrace();
        }
    }

    @GetMapping("/echarts")//对复杂表单处理
    public List<ShopDto> echarts(){
        List<ShopDto> shopDtos = service.getCountByState();
        return shopDtos;
    }

    @GetMapping("/active/{id}")//对复杂表单处理
    public AjaxResult emailPass(@PathVariable("id")Long id){
        try {
            service.emailPass(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"激活失败");
        }


    }
}
